package com.aphrodite.ui.graphics;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.aphrodite.image.ImageUtil;

/**
 * Animates a given image tile series.
 * 
 * @author kentelt
 * 
 */
public final class ImageTileAnimator {

	private int tileIndex;

	private final Image[] tiles;

	private int x;

	private int y;

	/**
	 * Creates a image tile animator instance using a horizontal tiled image
	 * 
	 * @param imageStrip
	 */
	public ImageTileAnimator(Image imageStrip, int number, int width, int height) {
		this(ImageUtil.getTiles(imageStrip, number, width, height));
	}

	public ImageTileAnimator(Image[] tiles) {
		this.tiles = tiles;
	}

	/**
	 * Call this method in a separate thread with predefined intervals
	 * 
	 * @param g
	 */
	public void paint(final Graphics g) {
		setTileIndex();
		g.drawImage(tiles[tileIndex], x, y, 0);
	}

	public void reset() {
		tileIndex = 0;
	}

	public void setLocation(int x, int y) {
		this.x = x;
		this.y = y;
	}

	private void setTileIndex() {
		tileIndex = (tileIndex < tiles.length - 1) ? tileIndex++ : 0;
	}
}
